package com.yanxi.controller;

import com.yanxi.anno.Log;
import com.yanxi.pojo.*;
import com.yanxi.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/students")
@RestController
public class StudentController {
    public static final Logger log = LoggerFactory.getLogger(StudentController .class);
    @Autowired
    private StudentService studentService;

    //分页查询班级学员
    @GetMapping
    public Result pageStudent(StudentQueryParam studentQueryParam){

       PageResult<Student> pageResult =  studentService.pageStudent(studentQueryParam);
       return Result.success(pageResult);
    }

    @Log
    //新增学员
    @PostMapping
    public Result saveStudent(@RequestBody Student student){
        log.info("新增学员:{}",student);
        studentService.saveStudent(student);
        return Result.success();
    }

    //根据id查询学员
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询学员，id:{}",id   );
        Student student = studentService.findById(id);
        return Result.success(student);
    }


    @Log
    //修改学员
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学员:{}",student);
        studentService.update(student);
        return Result.success();
    }

    @Log
    //批量删除学员
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除学员:{}",ids);
        studentService.delete(ids);
        return Result.success();
    }

    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Short score){
        log.info("违纪处理:{},{}",id,score);
        studentService.violation(id,score);
        return Result.success();
    }
}
